import type { VbenFormProps, VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { queryPage, setStatus } from './api';
import { ref } from 'vue';
import { z } from '#/adapter/form';

// 操作id
export const idKey = ref('linkId');
export const handleId = ref('');
export const htmlGroup = ref('1');

// 表格表单数据
export const formOptions: VbenFormProps = {
  schema: [
    {
      component: 'Input',
      defaultValue: '',
      fieldName: 'linkName',
      label: '友链名称',
    },
    {
      fieldName: 'sendStatus',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: [
          { label: '已显示', value: 1 },
          { label: '已隐藏', value: 0 },
        ],
      },
    },
  ],
  wrapperClass: 'grid-cols-5',
};

// 表格数据
export const gridOptions: VxeTableGridOptions = {
  rowConfig: {
    height: 80,
  },
  columns: [
    { title: '序号', type: 'seq', width: 80 },
    {
      title: 'LOGO',
      field: 'linkLogo',
      align: 'center',
      cellRender: {
        name: 'CellImageViewer',
        props: { width: '120px', height: '60px' },
      },
      width: 160,
    },
    { title: '名称', field: 'linkName', align: 'center', width: 200 },
    { title: '链接网址', slots: { default: 'web-url' }, align: 'left' },
    // {
    //   title: '显示',
    //   field: 'sendStatus',
    //   cellRender: {
    //     name: 'CellDict',
    //     props: { dictType: 'STATE' },
    //   },
    //   width: 100,
    // },
    {
      title: '显示',
      field: 'sendStatus',
      cellRender: {
        name: 'CellSwitch',
        props: {
          idKey: idKey.value,
          checkedValue: 1,
          changeQuery: setStatus,
        },
      },
      width: 120,
    },
    { title: '排序', field: 'sortOrder', align: 'center', width: 100 },
    { title: '创建时间', field: 'createTime', align: 'left', width: 200 },
    { slots: { default: 'action' }, title: '操作', width: 160 },
  ],
  minHeight: '100%',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        return await queryPage({
          page: page.currentPage,
          limit: page.pageSize,
          htmlGroup: htmlGroup.value,
          ...formValues,
        });
      },
    },
    response: {
      result: 'data.records',
      total: 'data.total',
      list: 'data.records',
    },
  },
  pagerConfig: {
    enabled: true,
    pageSize: 20,
  },
  toolbarConfig: {
    custom: true,
    export: false,
    import: false,
    refresh: true,
    zoom: true,
  },
};

// 编辑弹窗表单配置
export const drawerFormSchema: VbenFormSchema[] = [
  {
    component: 'Input',
    defaultValue: '',
    fieldName: idKey.value,
    label: '友情链接id',
    dependencies: {
      show() {
        return false;
      },
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入名称',
      allowClear: true,
    },
    fieldName: 'linkName',
    label: '友链名称',
    rules: 'required',
  },
  {
    component: 'ImageUpload',
    fieldName: 'linkLogo',
    help: '图片建议尺寸220*70',
    label: '友链LOGO',
    rules: z.string().min(1, { message: '请上传友链LOGO，图片建议尺寸220*70' }),
  },
  // {
  //   component: 'FileUpload',
  //   fieldName: 'linkLogo',
  //   help: '图片建议尺寸220*70',
  //   label: '友链LOGO',
  //   rules: z.string().min(1, { message: '请上传友链LOGO，图片建议尺寸220*70' }),
  // },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入网址前缀，https://或http://',
      allowClear: true,
    },
    help: '请输入网址前缀，https://或http://',
    fieldName: 'linkPrefix',
    label: '网址前缀',
    rules: 'required',
    defaultValue: 'https://',
  },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入网站域名，例如：www.****.com',
      allowClear: true,
    },
    help: '网站域名，例如：www.****.com',
    fieldName: 'linkUrl',
    label: '网站域名',
    rules: 'required',
  },
  // {
  //   component: 'RadioGroup',
  //   componentProps: {
  //     options: [
  //       {
  //         label: '显示',
  //         value: 1,
  //       },
  //       {
  //         label: '隐藏',
  //         value: 0,
  //       },
  //     ],
  //   },
  //   fieldName: 'sendStatus',
  //   defaultValue: 1,
  //   label: '状态',
  //   rules: 'required',
  // },
  {
    component: 'InputNumber',
    fieldName: 'sortOrder',
    componentProps: {},
    label: '排序值',
    rules: 'required',
    defaultValue: 99,
  },
];
